package com.sell.controller;

import com.sell.common.HttpResult;
import com.sell.dto.OrderDTO;
import com.sell.exceptions.ResultEnum;
import com.sell.exceptions.SellException;
import com.sell.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: megan
 * @eMail: meganmail@vip.qq.com
 * @date: 2018/12/14 14:24
 * @version: v1.0
 * @description: 卖家端订单
 */
@Slf4j
@Controller
@RequestMapping("/seller/order/")
public class SellerOrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 订单列表
     *
     * @param page 第几页，从第一页开始
     * @param size 一页多少行
     * @return ModelAndView
     */
    @GetMapping("list")
    public ModelAndView list(@RequestParam(value = "page", defaultValue = "1", required = false) Integer page,
                             @RequestParam(value = "size", defaultValue = "10", required = false) Integer size,
                             Map<String, Object> resultMap) {
        PageRequest pageRequest = new PageRequest(page - 1, size);
        Page<OrderDTO> orderDTOPage = orderService.findList(pageRequest);
        resultMap.put("orderDTOPage", orderDTOPage);
        resultMap.put("currentPage", page);
        resultMap.put("size", size);
        return new ModelAndView("order/list", resultMap);
    }

    @GetMapping("/cancel")
    public ModelAndView cancel(@RequestParam("orderId") String orderId, Map<String, Object> map) {
        OrderDTO dto;
        try {
            dto = orderService.findOne(orderId);
            orderService.cancel(dto);
        } catch (SellException e) {
            log.error("【卖家端取消订单】没有查到该订单 orderId：{} ", orderId);
            map.put("msg", e.getMessage());
            map.put("url", "/sell/seller/order/list");
            return new ModelAndView("/common/error", map);
        }
        map.put("msg", ResultEnum.ORDER_CANCEL_SUCCESS.getMessage());
        map.put("url", "/sell/seller/order/list");
        return new ModelAndView("/common/success", map);
    }
}
